﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _search : System.Web.UI.Page
{
    public string search_smallclassid = "";
    public string search_area = "";
    public string search_keyname = "";
    public string wheresql = "";
    public string wheresql2 = "";
    public string wheresql3 = "";
    public string query = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(Request.Form["keyname"].ToString());
		//Response.End();
		if (!IsPostBack)
        {
            
            if (Request.Form["smallclassid"] != null) { search_smallclassid = Request.Form["smallclassid"].ToString(); }
            if (IsNumeric(search_smallclassid) == false) { search_smallclassid = "1"; }
            if (Request.Form["area"] != null) { search_area = Request.Form["area"].ToString(); }
            if (Request.Form["keyname"] != null) { search_keyname = Request.Form["keyname"].ToString(); }
            search_area = htmlstr(search_area);
            search_keyname = htmlstr(search_keyname);
            //keyname.Text = search_keyname;

            if (search_smallclassid != "" || search_area != "" || search_keyname != "")
            {
//                Response.Write(search_smallclassid);
//				Response.End();
				searchinfo();
            }

            //ViewState["sort"] = " order by addtime desc";
        }
        
    }
    public void searchinfo()
    {
        query = "smallclassid=" + search_smallclassid + "area=" + search_area + "&keyname=" + search_keyname;

        if (search_smallclassid == "0") { wheresql2 = ""; }
        if (search_smallclassid != "0") { wheresql2 = " and smallclassid=" + search_smallclassid + ""; }

        if (search_area == "all") { wheresql = "where  (Title like '%" + search_keyname + "%' or Content like '%" + search_keyname + "%')" + wheresql2; }
        if (search_area == "title") { wheresql = "where  Title like '%" + search_keyname + "%'" + wheresql2; }
        if (search_area == "content") { wheresql = "where  Content like '%" + search_keyname + "%'" + wheresql2; }

        Page_newsList.RecordCount = (int)Socut.Data.ExecuteScalar("select count(*) from web_notice " + wheresql + " " );
        ViewState["strwhere"] = wheresql;


    }

    protected void Page_newsList_PageChanged(object sender, EventArgs e)
    {

        ShowData();
    }


    protected void ShowData()
    {
        DataSet ds = Socut.Data.ExecuteDataSet("select * from web_notice " + ViewState["strwhere"] + " order by addtime desc " ,Page_newsList.PageSize*(Page_newsList.CurrentPageIndex-1),Page_newsList.PageSize);
       
        //Response.End();
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }


    public string htmlstr(string chr)
    {
        if (chr == null)
            return "";
        chr = chr.Replace("<", "&lt");
        chr = chr.Replace(">", "&gt");
        chr = chr.Replace("\n", "<br>");
        chr = chr.Replace("\"", "&quot;");
        chr = chr.Replace("'", "&#39;");
        chr = chr.Replace(" ", "&nbsp;");
        chr = chr.Replace("select", "");
        chr = chr.Replace("delete", "");
        chr = chr.Replace("update", "");
        chr = chr.Replace("drop", "");
        chr = chr.Replace(";", "");
        chr = chr.Replace("(", "");
        chr = chr.Replace(")", "");
        chr = chr.Replace("and", "");
        chr = chr.Replace("or", "");
        chr = chr.Replace("\r", "");
        return (chr);
    }

    static bool IsNumeric(string str)
    {
        if (str == null || str.Length == 0)
            return false;
        foreach (char c in str)
        {
            if (!Char.IsNumber(c))
            {
                return false;
            }
        }
        return true;
    }
}
